#include "fileLoad.h"
#include <iostream>
#include <fstream>


int fileLoad(trainData* d, char* filePath, valueItem (*funParse)(char*))
{
    std::fstream adultFile;
    adultFile.open(filePath, std::ios::in);
    char tmpLine[300];
    //adultFile >> tmpLine;
    //adultFile >> tmpLine;
    for(int t = 0; t < 300; t ++)
    {
        tmpLine[t] = '\0';
    }


    adultFile.getline(tmpLine, 300);
    adultFile.getline(tmpLine, 300);

    /*
    for(int t = 0; tmpLine[t] != '\0'; t ++)
    {
        std::cout << (int)tmpLine[t] << " ";
    }
    std::cout << (int) '\n' << std::endl;
    */

    while(adultFile.eof() != 1 && tmpLine[0] != '\r')
    {
        //d->addItem(adultFileParse(tmpLine));
        d->addItem(funParse(tmpLine));
        //d->showData();
        //std::cout << tmpLine << std::endl;
        //std::cout << (int)tmpLine[0] << " " << (int)tmpLine[1] << std::endl;
        adultFile.getline(tmpLine, 300);
    }

    return 1;
}


valueItem adultFileParse(char* l)
{
    int frontChar = 0;
    int finalChar = 0;

    valueItem tmpValueItem;
    keyValue tmpKeyValue;
    tmpValueItem.setKeyValueNum(15);

    char tmpStr[20];
    int tmpInt;

    int count = 1;
    while(l[finalChar] != '\r')
    {
        finalChar = frontChar;
        while(l[finalChar] != ',' && l[finalChar] != '\r')
        {
            finalChar ++;
        }
        strCut(tmpStr, l, frontChar, finalChar);

        switch(count)
        {
        case 1:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"age", tmpInt, NULL);
            //tmpValueItem.addKeyValue(&tmpKeyValue);
            break;
        case 2:
            tmpKeyValue.setValue(0, (char*)"workclass", 0, tmpStr);
            break;
        case 3:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"fnlwgt", tmpInt, NULL);
            break;
        case 4:
            tmpKeyValue.setValue(0, (char*)"education", 0, tmpStr);
            break;
        case 5:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"education-num", tmpInt, NULL);
            break;
        case 6:
            tmpKeyValue.setValue(0, (char*)"marital-status", 0, tmpStr);
            break;
        case 7:
            tmpKeyValue.setValue(0, (char*)"occupation", 0, tmpStr);
            break;
        case 8:
            tmpKeyValue.setValue(0, (char*)"relationship", 0, tmpStr);
            break;
        case 9:
            tmpKeyValue.setValue(0, (char*)"race", 0, tmpStr);
            break;
        case 10:
            tmpKeyValue.setValue(0, (char*)"sex", 0, tmpStr);
            break;
        case 11:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"capital-gain", tmpInt, NULL);
            break;
        case 12:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"capital-loss", tmpInt, NULL);
            break;
        case 13:
            std::sscanf(tmpStr, "%d", &tmpInt);
            tmpKeyValue.setValue(1, (char*)"hours-per-work", tmpInt, NULL);
            break;
        case 14:
            tmpKeyValue.setValue(0, (char*)"native-country", 0, tmpStr);
            break;
        case 15:
            tmpKeyValue.setValue(0, (char*)"class", 0, tmpStr);
            break;
        }//switch
        frontChar = finalChar + 2;
        //finalChar = frontChar;
        tmpValueItem.addKeyValue(&tmpKeyValue);
        count ++;

    }//while

    return tmpValueItem;

}


int adultFileLoad(trainData* d, char* filePath)
{
    fileLoad(d, filePath, adultFileParse);
    return 1;
}


int carFileLoad(trainData* d, char* filePath)
{
    fileLoad(d, filePath, carFileParse);
    return 1;
}
valueItem carFileParse(char* l)
{
    int frontChar = 0;
    int finalChar = 0;

    valueItem tmpValueItem;
    keyValue tmpKeyValue;
    tmpValueItem.setKeyValueNum(7);

    char tmpStr[20];
    //int tmpInt;

    int count = 1;
    while(l[finalChar] != '\r')
    {
        finalChar = frontChar;
        while(l[finalChar] != ',' && l[finalChar] != '\r')
        {
            finalChar ++;
        }
        strCut(tmpStr, l, frontChar, finalChar);

        switch(count)
        {
        case 1:
            tmpKeyValue.setValue(0, (char*)"id1", 0, tmpStr);
            //tmpValueItem.addKeyValue(&tmpKeyValue);
            break;
        case 2:
            tmpKeyValue.setValue(0, (char*)"id2", 0, tmpStr);
            break;
        case 3:
            tmpKeyValue.setValue(0, (char*)"id3", 0, tmpStr);
            break;
        case 4:
            tmpKeyValue.setValue(0, (char*)"id4", 0, tmpStr);
            break;
        case 5:
            tmpKeyValue.setValue(0, (char*)"id5", 0, tmpStr);
            break;
        case 6:
            tmpKeyValue.setValue(0, (char*)"id6", 0, tmpStr);
            break;
        case 7:
            tmpKeyValue.setValue(0, (char*)"class", 0, tmpStr);
            break;
        }//switch
        frontChar = finalChar + 1;
        //finalChar = frontChar;
        tmpValueItem.addKeyValue(&tmpKeyValue);
        count ++;

    }//while

    return tmpValueItem;

}

int expandedFileLoad(trainData* d, char* filePath)
{
    fileLoad(d, filePath, expandedFileParse);
    return 1;
}
valueItem expandedFileParse(char* l)
{
    int frontChar = 0;
    int finalChar = 0;

    valueItem tmpValueItem;
    keyValue tmpKeyValue;
    tmpValueItem.setKeyValueNum(23);

    char tmpStr[20];
    //int tmpInt;

    int count = 1;
    while(l[finalChar] != '\r')
    {
        finalChar = frontChar;
        while(l[finalChar] != ',' && l[finalChar] != '\r')
        {
            finalChar ++;
        }
        strCut(tmpStr, l, frontChar, finalChar);

        switch(count)
        {
        case 1:
            tmpKeyValue.setValue(0, (char*)"id1", 0, tmpStr);
            //tmpValueItem.addKeyValue(&tmpKeyValue);
            break;
        case 2:
            tmpKeyValue.setValue(0, (char*)"id2", 0, tmpStr);
            break;
        case 3:
            tmpKeyValue.setValue(0, (char*)"id3", 0, tmpStr);
            break;
        case 4:
            tmpKeyValue.setValue(0, (char*)"id4", 0, tmpStr);
            break;
        case 5:
            tmpKeyValue.setValue(0, (char*)"id5", 0, tmpStr);
            break;
        case 6:
            tmpKeyValue.setValue(0, (char*)"id6", 0, tmpStr);
            break;
        case 7:
            tmpKeyValue.setValue(0, (char*)"id7", 0, tmpStr);
            break;
        case 8:
            tmpKeyValue.setValue(0, (char*)"id8", 0, tmpStr);
            break;
        case 9:
            tmpKeyValue.setValue(0, (char*)"id9", 0, tmpStr);
            break;
        case 10:
            tmpKeyValue.setValue(0, (char*)"id10", 0, tmpStr);
            break;
        case 11:
            tmpKeyValue.setValue(0, (char*)"id11", 0, tmpStr);
            break;
        case 12:
            tmpKeyValue.setValue(0, (char*)"id12", 0, tmpStr);
            break;
        case 13:
            tmpKeyValue.setValue(0, (char*)"id13", 0, tmpStr);
            break;
        case 14:
            tmpKeyValue.setValue(0, (char*)"id14", 0, tmpStr);
            break;
        case 15:
            tmpKeyValue.setValue(0, (char*)"id15", 0, tmpStr);
            break;
        case 16:
            tmpKeyValue.setValue(0, (char*)"id16", 0, tmpStr);
            break;
        case 17:
            tmpKeyValue.setValue(0, (char*)"id17", 0, tmpStr);
            break;
        case 18:
            tmpKeyValue.setValue(0, (char*)"id18", 0, tmpStr);
            break;
        case 19:
            tmpKeyValue.setValue(0, (char*)"id19", 0, tmpStr);
            break;
        case 20:
            tmpKeyValue.setValue(0, (char*)"id20", 0, tmpStr);
            break;
        case 21:
            tmpKeyValue.setValue(0, (char*)"id21", 0, tmpStr);
            break;
        case 22:
            tmpKeyValue.setValue(0, (char*)"id22", 0, tmpStr);
            break;
        case 23:
            tmpKeyValue.setValue(0, (char*)"class", 0, tmpStr);
            break;
        }//switch
        frontChar = finalChar + 1;
        //finalChar = frontChar;
        tmpValueItem.addKeyValue(&tmpKeyValue);
        count ++;

    }//while

    return tmpValueItem;

}



int strCut(char* dest, char* ori, int begin, int end)
{
    for(int t1 = begin, t2 = 0; t1 < end; t1 ++, t2 ++)
    {
        dest[t2] = ori[t1];
    }
    dest[end - begin] = '\0';
    return 1;

}
